#include "StaticItem.h"
#include "../proxy/ItemProxy.h"
#include "../../utils/LocalString.h"

void StaticItem::parse()
{
	const char* path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("config/Equipage.xml");

	xmlDocPtr equipDoc = xmlReadFile(path, "utf-8", XML_PARSER_EOF);
	if(NULL == equipDoc)
		CCLOG("Dungeons xml parse failed!");

	xmlNodePtr equipRootNode = xmlDocGetRootElement(equipDoc);
	if(NULL == equipRootNode)
		CCLOG("Dungeons xml root null!");

	xmlNodePtr equipChildNode = equipRootNode->children->next->last;

	xmlNodePtr curNode = equipChildNode;
	while(NULL != curNode)
	{
		if (curNode->type == XML_COMMENT_NODE)break;
		ItemStatic* itemStatic;
		ItemKind itemKind = (ItemKind)attriToInt(curNode, "kind");
		if (itemKind == ITEM_KIND_EQUIP)
		{
			EquipStatic* equipInfo = new EquipStatic();
			equipInfo->quality = attriToInt(curNode, "quality");
			equipInfo->atk_max = attriToInt(curNode, "atk_max");
			equipInfo->atk_min = attriToInt(curNode, "atk_min");
			equipInfo->def_max = attriToInt(curNode, "def_max");
			equipInfo->def_min = attriToInt(curNode, "def_min");
			equipInfo->life_max = attriToInt(curNode, "life_max");
			equipInfo->life_min = attriToInt(curNode, "life_min");
			equipInfo->fusion = attriToInt(curNode, "fusion");
			equipInfo->intensify_price = attriToInt(curNode, "intensify_price");
			equipInfo->intensify_energy = attriToInt(curNode, "intensify_energy");
			equipInfo->intensify_fusion = attriToInt(curNode, "intensify_fusion");
			equipInfo->rule = attriToInt(curNode, "rule");
			equipInfo->id = attriToInt(curNode, "id");
			equipInfoMap[equipInfo->id] = equipInfo;
			itemStatic = equipInfo;
		}
		else
		{
			itemStatic = new ItemStatic();
		}

		itemStatic->icon = attriToChar(curNode, "icon");
		itemStatic->name = attriToChar(curNode, "name");
		itemStatic->id = attriToInt(curNode, "id");
		itemStatic->kind = itemKind;

		std::string s1 = attriToChar(curNode,"buy");
		s1 = s1.substr(1, s1.length()-2);
		std::vector<std::string> idList = StringUtil::split(s1,", ");
		itemStatic->buyGold = strToInt(idList[0]);
		itemStatic->buyMoney = strToInt(idList[1]);
		itemStatic->sell = attriToInt(curNode, "sell");
		mItemInfoMap[itemStatic->id] = itemStatic;
		
		curNode = curNode->prev;
	}

	xmlFreeDoc(equipDoc);
}

EquipStatic* StaticItem::getEquipInfo(int id)
{
	if (equipInfoMap.count(id) == 0)
		return NULL;
	return equipInfoMap[id];
}

ItemStatic* StaticItem::getItemInfo( int id )
{
	if (mItemInfoMap.count(id) == 0)
		return NULL;
	return mItemInfoMap[id];
}

bool StaticItem::isAttack( int id )
{
	EquipStatic* equipInfo = getEquipInfo(id); 
	return equipInfo->type == EQUIP_PART_HELM ||
		 equipInfo->type == EQUIP_PART_NECKLACE || 
		 equipInfo->type == EQUIP_PART_WEAPON;
}

const char* StaticItem::getPropertyTitle( int id )
{
	return isAttack(id) ? gls("attack") : gls("defense");
}

const char* StaticItem::getTypeTitle( int id )
{
	EquipStatic* itemInfo = getEquipInfo(id);
	switch(itemInfo->type)
	{
	case EQUIP_PART_HELM:
		return gls("helm");
		break;
	case EQUIP_PART_NECKLACE:
		return gls("necklace");
		break;
	case EQUIP_PART_WEAPON:
		return gls("weapon");
		break;
	case EQUIP_PART_CLOTH:
		return gls("cloth");
		break;
	case EQUIP_PART_RING:
		return gls("ring");
		break;
	case EQUIP_PART_SHOES:
		return gls("shoes");
		break;
	}
	return NULL;
}
